function [data, yield, bench_yield, pos]=get_spread_data(raw)
% function [data, yield, bench_yield]=get_spread_data(raw)
% Computes spread data from Excel input
% Inputs:
%       raw     [T by 3] cell       cell array with columns [yield date benchmark_yield]
% Outputs:
%       data    [N by 4] matrix     data matrix with columns [date yield benchmark_yield spread]
%                                   the number of observations N is reduced
%                                   to the non-NaNs

% Copyright (C) 2014-19 Benjamin Born and Johannes Pfeifer 
%
% This is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This code is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% For a copy of the licencse,
% see <http://www.gnu.org/licenses/>.

pos.date=1;
pos.yield=2;
pos.bench=3;
pos.spread=4;


%transform Excel dates
if isnumeric(raw{1,2})
    dates_overall = x2mdate(cell2mat(raw(:, 2)));
else
    dates_overall=datenum(raw(:,2),'dd.mm.yyyy');
end
%read out yields and dates
yield=cell2mat(raw(:,1));
bench_yield=cell2mat(raw(:,3));
%get rid of NaN
dates_yield=dates_overall(~isnan(yield));
yield=yield(~isnan(yield));
dates_bench=dates_overall(~isnan(bench_yield));
bench_yield=bench_yield(~isnan(bench_yield));

%get starting and end dates for current bond and write the yields to a
%matrix
start_date=min([dates_yield;dates_bench ]);
end_date=max([dates_yield;dates_bench ]);
daily_timeline=(start_date:end_date)';
data=[daily_timeline, NaN(length(daily_timeline),2)]; % add two NaN 
data(ismember(daily_timeline,dates_yield),2)=yield;
data(ismember(daily_timeline,dates_bench),3)=bench_yield;

% clean NaNs to leave only common data points
data(any(isnan(data(:,2:3)),2),:)=[];
yield=data(:,pos.yield);
bench_yield=data(:,pos.bench);
%compute spreads and add them to last row
data=[data data(:,pos.yield)-data(:,pos.bench)];

